﻿<?php
/**
 * @author HuyHoang
 * @copyright 2011
 */

class CustomersController extends AppController {
    var $name = 'Customers';
    var $helpers = array('Html','Ajax','Javascript','Form');
    var $paginate = array(
        'limit' => 10,
        'order' => array('Customer.lastname' => 'asc')
    );
    var $components = array('RequestHandler');
    
    function upload_image($id = null) {
        
        $this->Customer->id = $id;
        $cust = $this->Customer->read();
        if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") 
        || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 2000000))
          {
            
          /*if ($_FILES["file"]["error"] > 0)
            {
            echo "Error: " . $_FILES["file"]["error"] . "<br />";
            }
          else
            {
            echo "Upload: " . $_FILES["file"]["name"] . "<br />";
            echo "Type: " . $_FILES["file"]["type"] . "<br />";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
            echo "Stored in: " . $_FILES["file"]["tmp_name"];*/
            move_uploaded_file($_FILES['file']['tmp_name'], 'E:\\test.jpg');
        //    }
          }
        else
          {
          echo "Invalid file";
          }
    }
    
    function index($id = null) {
        $this->layout = "home";

        $this->Customer->id = $id;
        $this->set('iCustomer', $this->Customer->read());

        $data = $this->paginate('Customer', array('deleteStatus' => 0));
        $this->set('customers', $data);
    }
    
    function save() {
        $id = $_REQUEST['id'];
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];
        $lastname = $_REQUEST['lastname'];
        $firstname = $_REQUEST['firstname'];
        $birthday = $_REQUEST['birthday'];
        $address = $_REQUEST['address'];
        $email = $_REQUEST['email'];
        $phone = $_REQUEST['phone'];
        $status = $_REQUEST['status'];
        
        if (($id != null) && ($id != "")) {
            $this->Customer->id = $id;
            $this->Customer->set('username', $username);
            $this->Customer->set('password', $password);
            $this->Customer->set('lastname', $lastname);
            $this->Customer->set('firstname', $firstname);
            if (($birthday != null) && ($birthday != "")) $this->Customer->set('birthday', date("Y-m-d", strtotime($birthday)));
            $this->Customer->set('address', $address);
            $this->Customer->set('email', $email);
            $this->Customer->set('phone', $phone);
            $this->Customer->set('currentStatus', $status);
            
            $this->Customer->save();
        } else {
            $this->Customer->create();
            $this->Customer->set('username', $username);
            $this->Customer->set('password', $password);
            $this->Customer->set('lastname', $lastname);
            $this->Customer->set('firstname', $firstname);
            if (($birthday != null) && ($birthday != "")) $this->Customer->set('birthday', date("Y-m-d", strtotime($birthday)));
            $this->Customer->set('address', $address);
            $this->Customer->set('email', $email);
            $this->Customer->set('phone', $phone);
            $this->Customer->set('currentStatus', $status);
            $this->Customer->saveAll();
        }
        $this->redirect(array("action" => "index", $this->Customer->id));
    }
    
    function delete($id = null) {
        $this->Customer->id = $id;
        $this->Customer->saveField("deleteStatus", "1");
        $this->redirect(array("action" => "index"));
    }
}

?>
